#!/bin/sh
# Copyright (c) 2011, bkil.hu
# This program is free software and can be distributed under the terms of
# the GNU General Public License v2,
# see COPYING for detailed licensing terms.

# It displays aggregate disk usage of your home directory (if no args)
# or the given directory.

display() {
 local DIR=$1
 du -sk $DIR/..?* $DIR/.[!.]* $DIR/* 2>/dev/null |
  sort -n |
  awk '
   {
    size = $1;
    sum += size;
    name = $2;
    if ($1 < 1024){
     printf("%9ik %s\n",size,name);
    } else {
     printf("%9.3fM %s\n",size/1024,name);
    }
   }
   END{
    print sum " kiB"
   }
  '
}

main() {
 local DIR=$1
 if [ "$DIR" = "" ]
 then
  display $HOME
  quota
 else
  display $DIR
 fi
}

main "$@"

